﻿using System;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using uTrade.Controls;
using uTrade.Core;
using uTrade.ViewModels;

namespace uTrade.Views
{
    /// <summary>
    /// Interaction logic for TechView.xaml
    /// </summary>
    ///
    public partial class ChartView : UserControl
    {
        private readonly ChartPanel oCandleCanvas = new ChartPanel();
        private ChartPanel oVolumeCanvas = new ChartPanel();
        private ChartPanel oTimeCavas = new ChartPanel();

        private CandleLine candleLine = new CandleLine();
        private ZereVerticalLine zereVertical = new ZereVerticalLine();

        private CrossLine crossLine = new CrossLine();

        private ChartViewModel viewModel;

        //DrawingRegion drawingRegion = new DrawingRegion();

        private TransDatas stockData;

        //public delegate void PriceChangedEventHandler(DayPrice p);
        //public event PriceChangedEventHandler OnPriceChanged;

        public ChartView()
        {
            InitializeComponent();

            DataContext = viewModel = new ChartViewModel();

            this.klinepanel.Children.Add(oCandleCanvas);
            this.klinepanel.Children.Add(candleLine);
            this.klinepanel.Children.Add(crossLine);
            //this.klinepanel.Children.Add(drawingRegion);

            this.volumepanel.Children.Add(oVolumeCanvas);
            this.volumepanel.Children.Add(zereVertical);

            this.timepanel.Children.Add(oTimeCavas);

            candleLine.OnTestMouseMove += new DrawObjBase.MouseMoveEventHandler(Canvas_OnTestMouseMove);

            //this.oCandleCanvas.OnPriceChanged += new ChartPanel.PriceChangedEventHandler(Canvas_OnPriceChanged);
            //this.oCandleCanvas.OnRegionChanged += new ChartPanel.RegionChangedHandler(Canvas_OnRegionChanged);

            //this.oVolumeCanvas.OnPriceChanged += new ChartPanel.PriceChangedEventHandler(Canvas_OnPriceChanged);
            //this.oVolumeCanvas.OnRegionChanged += new ChartPanel.RegionChangedHandler(Canvas_OnRegionChanged);

            Init();
        }

        private void Canvas_OnTestMouseMove(object sender, object obj)
        {
            Point point = (Point)obj;
            viewModel.Open = point.X;
            viewModel.Close = point.Y;
        }

        public void Init()
        {
            LoadIndicators(Environment.CurrentDirectory + "//uTrade.Core.dll");
        }

        internal void Load(TransDatas tDatas)
        {
            stockData = tDatas;
            oCandleCanvas.Load();
            oVolumeCanvas.Load();
            oTimeCavas.Load();

            InitDrawingRegion();
            InitDrawingObjects();

            viewModel.Symbol = tDatas.Instrument;
            viewModel.Name = tDatas.Instrument;
            viewModel.High = tDatas.High.Max();
            viewModel.Low = tDatas.High.Min();

            //candleLine = new CandleLine
            {
                candleLine.Open = tDatas.Open;
                candleLine.High = tDatas.High;
                candleLine.Low = tDatas.Low;
                candleLine.Close = tDatas.Close;
            }

            {
                Indicator indicator = new Indicator();
                Core.MACD mACD = indicator.MACD(tDatas.Close, 12, 26, 9);
                DataSeries diff = mACD.Value;
                zereVertical.Data = diff;
            }

            //if (tDatas.StockInfo.Favorite)
            //{
            //    ImageSource imageUpnew = new BitmapImage(new Uri(@"/Resources/Images/favorite.png", UriKind.RelativeOrAbsolute));
            //    Image imgtemp = new Image();
            //    imgtemp.Source = imageUpnew;
            //    imgtemp.Width = 16;
            //    imgtemp.Height = 16;
            //    btn_Favorite.Content = imgtemp;

            //}
            //else
            //{
            //    ImageSource imageUpnew = new BitmapImage(new Uri(@"/Resources/Images/un-favorite.png", UriKind.RelativeOrAbsolute));
            //    Image imgtemp = new Image();
            //    imgtemp.Source = imageUpnew;
            //    imgtemp.Width = 16;
            //    imgtemp.Height = 16;
            //    btn_Favorite.Content = imgtemp;
            //}
        }

        private void InitDrawingObjects()
        {
            Data.Candle oCandle = new Data.Candle();
            Data.MACD oMacd = new Data.MACD();

            //oCandleCanvas.DrawingObjects.Add(oCandle.GetDrawingObj(oPriceInfo));
            //oVolumeCanvas.DrawingObjects.Add(oMacd.GetDrawingObj(oPriceInfo));
        }

        private void InitDrawingRegion()
        {
            oCandleCanvas.ScaleLineCount = 5;
            oVolumeCanvas.ScaleLineCount = 0;

            oVolumeCanvas.IsShowTime = false;
            oTimeCavas.IsShowTime = false;
        }

        /// <summary>
        /// 加载指标
        /// </summary>
        /// <param name="file">文件路径</param>
        private void LoadIndicators(string strFile)
        {
            try
            {
                Assembly ass = Assembly.LoadFile(strFile);
                //加载hf_plat报错:增加对hf_plat_core的引用
                foreach (var t in ass.GetTypes())
                {
                    if (t.BaseType.FullName == typeof(uTrade.Core.Indicator).FullName)
                    {
                        this.cmbx_Indicators.Items.Add(t);
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private void Canvas_OnPriceChanged(Data.DayPrice p)
        {
            oTittleBox.SetTittle(oCandleCanvas.DrawingObjects.GetFormattedText());
        }

        #region chartPanel Mouse Events

        /// <summary>
        /// 水平移动时隐藏交叉线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        ///

        private void ChartPanel_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var pos = e.GetPosition(this);
            //double chartOffset = oCandleCanvas.chartOffset;

            //var x = pos.X;
            //var y = pos.Y;
            //if (!oTimeCavas.InChart())
            //{
            //    return;
            //}

            ////in timeline area
            //var startIndex = (int)((x - oCandleCanvas.ChartStartX) / oCandleCanvas.ChartWidth * oCandleCanvas.TotalItemCount);
            //chartOffset = startIndex * (oCandleCanvas.itemWidth + oCandleCanvas.itemSpace) - oCandleCanvas.ChartWidth / 2.0;

            //oCandleCanvas.chartOffset = chartOffset;
            //oVolumeCanvas.chartOffset = chartOffset;
            //oTimeCavas.chartOffset = chartOffset;

            //oCandleCanvas.mouseDown(e);
            //oCandleCanvas.mouseDown(e);
            //oTimeCavas.mouseDown(e);
        }

        #endregion chartPanel Mouse Events

        private void Btn_Favorite_Click(object sender, RoutedEventArgs e)
        {
            //if (stockData.StockInfo.Favorite)
            //{
            //    stockData.StockInfo.Favorite = false;
            //    ImageSource imageUpnew = new BitmapImage(new Uri(@"/Resources/Images/un-favorite.png", UriKind.RelativeOrAbsolute));
            //    Image imgtemp = new Image();
            //    imgtemp.Source = imageUpnew;
            //    imgtemp.Width = 16;
            //    imgtemp.Height = 16;
            //    btn_Favorite.Content = imgtemp;
            //}
            //else
            //{
            //    stockData.StockInfo.Favorite = true;
            //    ImageSource imageUpnew = new BitmapImage(new Uri(@"/Resources/Images/favorite.png", UriKind.RelativeOrAbsolute));
            //    Image imgtemp = new Image();
            //    imgtemp.Source = imageUpnew;
            //    imgtemp.Width = 16;
            //    imgtemp.Height = 16;
            //    btn_Favorite.Content = imgtemp;
            //}
        }

        private void Cmbx_Indicators_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        }
    }
}